<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\store\controller\order;

use app\common\enum\order\OrderScene as OrderSceneEnum;
use app\common\service\order\Printer as PrinterService;
use think\response\Json;
use app\store\controller\Controller;
use app\store\model\order\Delivery as DeliveryModel;
use app\store\model\Order as OrderModel;
use app\store\model\OrderGoods as OrderGoodsModel;
use app\store\service\order\EOrder as EOrderService;
use app\store\service\order\Delivery as DeliveryService;

/**
 * 订单发货管理
 * Class Delivery
 * @package app\store\controller\order
 */
class Delivery extends Controller
{
    /**
     * 订单发货记录
     * @return Json
     */
    public function list(): Json
    {
        $model = new DeliveryModel;
        $list = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 发货单详情
     * @param int $deliveryId 发货单ID
     * @return Json
     */
    public function detail(int $deliveryId): Json
    {
        $detail = DeliveryModel::detail($deliveryId);
        return $this->renderSuccess(compact('detail'));
    }

    /**
     * 确认发货 (手机端确认)
     * @param int $orderId 订单ID
     * @return Json
     */
    public function accept(int $orderId): Json
    {
        $service = new DeliveryService;
        if ($service->accept($orderId)) {
            return $this->renderSuccess('接单成功');
        }
        return $this->renderError($service->getError() ?: '接单失败');
    }

    /**
     * 确认发货 (商家手机端)
     * @param int $order_id 订单ID
     * @return Json
     */
    public function deliveryBym(int $order_id): Json
    {
        //根据ordersn，获取Orderid，以及ordergoods重新组装调用原来的接口
        if(!$this->store){
            return $this->renderError('请先登录再核销');
        }
        if(!$order_id){
            return $this->renderError('请选择要配送的订单');
        }
        $login_store_id =  $this->store['store_id'];

        $data = array();
        $OrderModel = new OrderModel();
        $rs = $OrderModel->where(['order_id'=>$order_id])->find();
        if($rs) {
            $OrderGoodsModel = new OrderGoodsModel();
            if ($login_store_id !== $rs->store_id) {
                return $this->renderError('非本店订单，操作失败！');
            }
            $data['deliveryMethod'] = 20;//无需物流，门店自提
            $rs2 = $OrderGoodsModel->where(['order_id' => $order_id])->select();
            if ($rs2) {
                $arr = $rs2->toArray();
                foreach ($arr as $k => $v) {
                    $data['packGoodsData'][$k]['orderGoodsId'] = $v['order_goods_id'];
                    $data['packGoodsData'][$k]['deliveryNum'] = $v['total_num'];
                }
            }
            $data['expressNo'] = "";
            $data['syncMpWeixinShipping'] = 1;
            $service = new DeliveryService;
            if ($service->delivery($order_id, $data)) {
                //自动确认收货
                $model = $OrderModel::getOrderDetail($order_id);
                $model->receipt();
                return $this->renderSuccess('操作成功');
            }
            return $this->renderError($service->getError() ?: '操作失败');
        }
    }

    /**
     *
     * 确认发货 (手动录入)
     * @param int $orderId 订单ID
     * @return Json
     */
    public function delivery(int $orderId): Json
    {
        $service = new DeliveryService;
        if ($service->delivery($orderId, $this->postForm())) {
            return $this->renderSuccess('发货成功');

        }
        return $this->renderError($service->getError() ?: '发货失败');
    }

    /**
     * 确认发货 (扫码核销)
     * @param int $orderSn 订单ID
     * @return Json
     */
    public function deliverybyscan(int $orderNo): Json
    {

        //根据ordersn，获取Orderid，以及ordergoods重新组装调用原来的接口
        if(!$this->store){
            return $this->renderError('请先登录再核销');
        }

        $login_store_id =  $this->store['store_id'];

        $data = array();
        $OrderModel = new OrderModel();
        $OrderGoodsModel = new OrderGoodsModel();
        $rs = $OrderModel->where(['order_no'=>$orderNo])->find();
        if($rs){
            $orderId = $rs->order_id;
            if($login_store_id !== $rs->store_id){
                return $this->renderError('非本店订单，核销失败！');
            }
            $data['deliveryMethod'] = 20;//无需物流，门店自提
            $rs2 = $OrderGoodsModel->where(['order_id'=>$orderId])->select();
            if($rs2){
                $arr = $rs2->toArray();
                foreach ($arr as $k=>$v){
                    $data['packGoodsData'][$k]['orderGoodsId'] = $v['order_goods_id'];
                    $data['packGoodsData'][$k]['deliveryNum'] = $v['total_num'];
                }
            }
            $data['expressNo'] = "";
            $data['syncMpWeixinShipping'] = 1;
            $service = new DeliveryService;
            if ($service->delivery($orderId, $data)) {
                //核销完成并自动确认收货
                $model = $OrderModel::getOrderDetail($orderId,['user','goods','address']);
                $model->receipt();
                // 小票打印
                (new PrinterService)->printTicket($model->toArray(), OrderSceneEnum::PAYMENT);
                return $this->renderSuccess(['order_id'=>$orderId]);
            }
            return $this->renderError($service->getError() ?: '发货失败');
        }else{
            return $this->renderError('订单不存在，核销失败');
        }


    }
}